package com.yxhl.zoume.core.busticket.presenter;

import com.yxhl.protobuf.PubStartCityRequest;
import com.yxhl.protobuf.PubStartCityResponse;
import com.yxhl.protobuf.StartCityEntry;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.busticket.view.BusStartStationView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.busticket.GetStartCitiesUseCase;

import java.util.List;

import javax.inject.Inject;

/**
 * 获取车站列表的presenter
 * Created by vhawk on 16/3/20.
 */
public class StartCitiesPresenter implements IPresenter {

    private final GetStartCitiesUseCase getStartCitiesUseCase;
    private BusStartStationView busStartStationView;

    @Inject
    public StartCitiesPresenter(GetStartCitiesUseCase getStartCitiesUseCase) {
        this.getStartCitiesUseCase = getStartCitiesUseCase;
    }

    @Override
    public void attachView(BaseView view) {
        busStartStationView = (BusStartStationView) view;
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        getStartCitiesUseCase.unsubscribe();
    }

    /**
     * 加载巴士列表
     *
     * @param
     */
    public void fetchBusStations() {
        busStartStationView.waitingForQueryCities(true);

        PubStartCityRequest pubStartCityRequest = PubStartCityRequest
                .newBuilder()
                .setSerialVersionUID(System.currentTimeMillis())
                .build();

        getStartCitiesUseCase.execute(pubStartCityRequest, new ZMSubscriber<PubStartCityResponse>() {
            @Override
            public void onNext(PubStartCityResponse pubStartCityResponse) {
                super.onNext(pubStartCityResponse);
                List<StartCityEntry> startCityList = pubStartCityResponse.getStartCityFieldsList();
                if (startCityList != null && startCityList.size() > 0) {
                    busStartStationView.renderBusStationListResult(startCityList);
                } else {
                    busStartStationView.waitingForQueryCities(false);
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                busStartStationView.waitingForQueryCities(false);
            }
        });
    }
}
